package com.kkd.kkdapp.adapter;

import java.text.DecimalFormat;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.kkd.kkdapp.R;
import com.kkd.kkdapp.activity.BidDetailsActivity;
import com.kkd.kkdapp.activity.ExperienceParticularsActivity;
import com.kkd.kkdapp.activity.RegularParticularsActivity;
import com.kkd.kkdapp.activity.RepaymentDetailActivity;
import com.kkd.kkdapp.http.entity.TenderDetailMapList;
import com.kkd.kkdapp.listener.OnSingleClickListener;
import com.kkd.kkdapp.utils.SystemUtils;
import com.kkd.kkdapp.view.RoundProgressBar;

public class MyTiyanAdapter extends BaseAdapter {
	private List<TenderDetailMapList> mList;
	private Context context;
	private DecimalFormat df;

	public MyTiyanAdapter(Context context, List<TenderDetailMapList> mList) {
		this.mList = mList;
		this.context = context;
		df = new DecimalFormat("0.01");
	}

	@Override
	public int getCount() {
		return mList.size();
	}

	@Override
	public Object getItem(int arg0) {
		return mList.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = View.inflate(context, R.layout.myregular_other_item,
					null);
			holder.refund_name = (TextView) convertView
					.findViewById(R.id.refund_name);
			holder.interest_tv = (TextView) convertView
					.findViewById(R.id.interest_tv);
			holder.deadline_company_tv = (TextView) convertView
					.findViewById(R.id.deadline_company_tv);
			holder.bid = (TextView) convertView.findViewById(R.id.bid);
			holder.title = (RelativeLayout) convertView
					.findViewById(R.id.title);
			holder.top = (RelativeLayout) convertView.findViewById(R.id.top);
			holder.centre = (LinearLayout) convertView
					.findViewById(R.id.centre);
			holder.progressBar1 = (RoundProgressBar) convertView
					.findViewById(R.id.progressBar1);
			holder.term = (TextView) convertView.findViewById(R.id.term);
			holder.company_tv = (TextView) convertView
					.findViewById(R.id.company_tv);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		// 标的名称
		holder.refund_name.setText(mList.get(position).getBidTitle());
		// 投标期限
		holder.term.setText("" + mList.get(position).getBidPeriod());
		if ("天".equals(mList.get(position).getBidPeriodName2())) {
			holder.company_tv.setText(mList.get(position).getBidPeriodName2());
		} else if ("月".equals(mList.get(position).getBidPeriodName2())) {
			holder.company_tv.setText("个"
					+ mList.get(position).getBidPeriodName2());
		}
		// 投标金额
		holder.interest_tv.setText(mList.get(position).getPoiAmount());
		// 投标日期
		holder.deadline_company_tv.setText("投标时间 "
				+ mList.get(position).getPoiDate());
		// 进度条
		String bidAmount = mList.get(position).getBidAmount();
		String bidFreeAmount = mList.get(position).getBidFreeAmount();
		double interest = (Double.parseDouble(bidAmount) - Double
				.parseDouble(bidFreeAmount)) / Double.parseDouble(bidAmount);
		int interest1 = (int) (interest * 100);
		holder.progressBar1.setTextSize(SystemUtils.dip2px(context, 13));
		holder.progressBar1.setRoundWidth(SystemUtils.dip2px(context, 3));
		// 进度色值
		holder.progressBar1.setCricleProgressColor(Color.parseColor("#ff841f"));
		// 圆环色值
		holder.progressBar1.setCricleColor(Color.parseColor("#eeeeee"));
		// 进度字体色值
		holder.progressBar1.setTextColor(Color.parseColor("#ff841f"));
		holder.progressBar1.setPropress(mList.get(position).getBidProgress()
				+ "%");
		holder.progressBar1.setProgress((int) Double.parseDouble(mList.get(
				position).getBidProgress()));
		if (Double.parseDouble(mList.get(position).getBidProgress()) < 1) {
			holder.progressBar1.setProgress(1);
		} else if (Double.parseDouble(mList.get(position).getBidProgress()) > 1) {
			holder.progressBar1.setProgress(interest1);
		}
		// 标的状态
		final String bidStatusCode = mList.get(position).getBidStatusCode();
		if ("5002".equals(bidStatusCode)) {
			holder.bid.setVisibility(View.GONE);
			holder.progressBar1.setVisibility(View.VISIBLE);
		} else if ("5003".equals(bidStatusCode)) {
			holder.bid.setVisibility(View.VISIBLE);
			holder.bid.setBackgroundResource(R.drawable.tender_img_man);
			holder.progressBar1.setVisibility(View.GONE);
		} else if ("5004".equals(bidStatusCode)) {
			holder.bid.setVisibility(View.VISIBLE);
			holder.bid.setBackgroundResource(R.drawable.tender_img_returning);
			holder.progressBar1.setVisibility(View.GONE);
		} else if ("5005".equals(bidStatusCode)) {
			holder.bid.setVisibility(View.VISIBLE);
			holder.bid.setBackgroundResource(R.drawable.tender_img_returnd);
			holder.progressBar1.setVisibility(View.GONE);
		}
		// // 头部跳转详情
		// holder.title.setOnClickListener(new OnClickListener() {
		//
		// @Override
		// public void onClick(View v) {
		// String bidType = mList.get(position).getBidType();
		// String bidId = mList.get(position).getBidId();
		// String poiId = mList.get(position).getPoiId();
		// String appbiddetail = mList.get(position).getAppbiddetail();
		// if ("501".equals(bidType)) {
		// Intent intent = new Intent(context,
		// ExperienceParticularsActivity.class);
		// intent.putExtra("bidId", bidId);
		// intent.putExtra("poiId", poiId);
		// intent.putExtra("appbiddetail", appbiddetail);
		// context.startActivity(intent);
		// } else {
		// Intent intent = new Intent(context,
		// RegularParticularsActivity.class);
		// intent.putExtra("bidId", bidId);
		// intent.putExtra("poiId", poiId);
		// intent.putExtra("appbiddetail", appbiddetail);
		// context.startActivity(intent);
		// }
		// }
		// });
		// // 点击下部跳转还款详情
		// holder.centre.setOnClickListener(new OnClickListener() {
		//
		// @Override
		// public void onClick(View arg0) {
		// if (!"5002".equals(bidStatusCode)
		// && !"5003".equals(bidStatusCode)) {
		// String poiId = mList.get(position).getPoiId();
		// Intent intent = new Intent(context,
		// RepaymentDetailActivity.class);
		// intent.putExtra("poiId", poiId);
		// context.startActivity(intent);
		// }
		//
		// }
		// });
		holder.top.setOnClickListener(new OnSingleClickListener() {

			@Override
			public void onSingleClick(View v) {
				Intent intent = new Intent(context, BidDetailsActivity.class);
				intent.putExtra("poiId", mList.get(position).getPoiId());
				intent.putExtra("isTiyan", true);
				context.startActivity(intent);
			}
		});
		return convertView;
	}

	class ViewHolder {
		TextView refund_name;
		TextView interest_tv;
		TextView deadline_company_tv;
		TextView bid;
		RelativeLayout title, top;
		LinearLayout centre;
		RoundProgressBar progressBar1;
		TextView term, company_tv;
	}

}
